package com.slz3.business.module.goodsManagement.gooInWarehouse.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.response.ResInfo;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.goodsManagement.gooInWarehouse.domain.GooInWarehouse;
import com.slz3.business.module.goodsManagement.gooInWarehouse.domain.GooInWarehouseFactory;
import com.slz3.business.module.systemManagement.sysUser.steward.SysUserConverter;
import org.springframework.data.domain.Page;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.List;

public class GooInWarehouseResExecutor {

    protected Mono<ServerResponse> add(ServerRequest req, GooInWarehouse gooInWarehouse) {
        SysUserConverter.setSaveUpdateUser(req, gooInWarehouse, true);
        GooInWarehouse gooInWarehouseDBLatest = Services.gooInWarehouse.save(gooInWarehouse);
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(gooInWarehouseDBLatest).build());
    }

    /**
     * known: gooInWarehouseList is not null
     */
    protected Mono<ServerResponse> delete(ServerRequest req) {
        List<GooInWarehouse> gooInWarehouseList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(gooInWarehouseList)) {
            List<GooInWarehouse> gooInWarehouseListNew = Services.gooInWarehouse.delete(gooInWarehouseList);
            if (VE.isNotEmpty(gooInWarehouseListNew) && gooInWarehouseListNew.size() == gooInWarehouseList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(gooInWarehouseListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("delete fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> modify(ServerRequest req, GooInWarehouse source) {
        SysUserConverter.setSaveUpdateUser(req, source, false);
        GooInWarehouse target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            Steward.merge(source, target);
            GooInWarehouse gooInWarehouseDBLatest = Services.gooInWarehouse.modify(target);
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(gooInWarehouseDBLatest).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> list(ServerRequest req) {
        Page<GooInWarehouse> gooInWarehousePage = Services.gooInWarehouse.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(gooInWarehousePage).build());
    }

    protected Mono<ServerResponse> info(ServerRequest req) {
        GooInWarehouse target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(target).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> choice(ServerRequest req) {
        Page<GooInWarehouse> gooInWarehousePage = Services.gooInWarehouse.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(gooInWarehousePage).build());
    }

    /**
     * known: gooInWarehouseList is not null
     */
    protected Mono<ServerResponse> auditSubmit(ServerRequest req) {
        List<GooInWarehouse> gooInWarehouseList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(gooInWarehouseList)) {
            gooInWarehouseList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.WAIT_AUDIT);
                GooInWarehouseFactory.initBaseFields(item);
            });
            List<GooInWarehouse> gooInWarehouseListNew = Services.gooInWarehouseTransactional.batchModify(gooInWarehouseList);
            if (VE.isNotEmpty(gooInWarehouseListNew) && gooInWarehouseListNew.size() == gooInWarehouseList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(gooInWarehouseListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditSubmit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: gooInWarehouseList is not null
     */
    protected Mono<ServerResponse> audit(ServerRequest req) {
        List<GooInWarehouse> gooInWarehouseList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(gooInWarehouseList)) {
            gooInWarehouseList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.APPROVED);
                GooInWarehouseFactory.initBaseFields(item);
            });
            List<GooInWarehouse> gooInWarehouseListNew = Services.gooInWarehouseTransactional.batchModify(gooInWarehouseList);
            if (VE.isNotEmpty(gooInWarehouseListNew) && gooInWarehouseListNew.size() == gooInWarehouseList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(gooInWarehouseListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("audit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: gooInWarehouseList is not null
     */
    protected Mono<ServerResponse> auditReject(ServerRequest req) {
        List<GooInWarehouse> gooInWarehouseList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(gooInWarehouseList)) {
            gooInWarehouseList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.REJECT);
                GooInWarehouseFactory.initBaseFields(item);
            });
            List<GooInWarehouse> gooInWarehouseListNew = Services.gooInWarehouseTransactional.batchModify(gooInWarehouseList);
            if (VE.isNotEmpty(gooInWarehouseListNew) && gooInWarehouseListNew.size() == gooInWarehouseList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(gooInWarehouseListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: gooInWarehouseList is not null
     */
    protected Mono<ServerResponse> usingEnable(ServerRequest req) {
        List<GooInWarehouse> gooInWarehouseList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(gooInWarehouseList)) {
            gooInWarehouseList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.ENABLE);
                GooInWarehouseFactory.initBaseFields(item);
            });
            List<GooInWarehouse> gooInWarehouseListNew = Services.gooInWarehouseTransactional.batchModify(gooInWarehouseList);
            if (VE.isNotEmpty(gooInWarehouseListNew) && gooInWarehouseListNew.size() == gooInWarehouseList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(gooInWarehouseListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }


    /**
     * known: gooInWarehouseList is not null
     */
    protected Mono<ServerResponse> usingDisable(ServerRequest req) {
        List<GooInWarehouse> gooInWarehouseList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(gooInWarehouseList)) {
            gooInWarehouseList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.DISABLE);
                GooInWarehouseFactory.initBaseFields(item);
            });
            List<GooInWarehouse> gooInWarehouseListNew = Services.gooInWarehouseTransactional.batchModify(gooInWarehouseList);
            if (VE.isNotEmpty(gooInWarehouseListNew) && gooInWarehouseListNew.size() == gooInWarehouseList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(gooInWarehouseListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("usingDisable fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

}
